programming4us
           
 
 
SQL Server

Programming with SQL Azure : Connecting to SQL Azure (part 3) - ODBC

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
1/3/2011 9:14:23 AM

2. ODBC

There is nothing earth-shattering or truly groundbreaking here, but let's walk though an example to see how ODBC connections work and illustrate that your ODBC classes still work as you're used to. Follow these steps:

  1. Do this the proper way and create an enumeration to handle the type of connection you're using.

  2. Modify the GetConString method as shown in the following snippet to take a parameter. The parameter lets you specify the connection type so you can return the correct type of connection string (either ADO.NET or ODBC). Be sure to use your correct password and server name with the correct server. If the value of ADO_NET is passed into this method, the ADO.NET connection string is returned; otherwise the ODBC connection string is returned:

    enum ConnType
    {
    ADO_NET = 1,
    ODBC = 2
    }
    string GetConString(ConnType connType)
    {
    if (connType == ConnType.ADO_NET)
    return "Server=tcp:servername.database.windows.net;Database=TechBio;
    User ID=SQLScott@servername;Password=password;
    Trusted_Connection=False;Encrypt=True;";
    else
    return "Driver={SQL Server Native Client 10.0};Server=tcp:servername.database.windows.net;
    Database=TechBio;Uid=SQLScott@servername;Pwd=password;Encrypt=yes;";
    }


  3. Place a second button on the form, along with a DataGridView. In its click event, add the following code. This code is just like the code from the ADO.NET example, but it uses the Odbc data classes versus the Sql data classes. For clarity, change the Text property of this new button to "ODBC" so you know the difference between this button and the first button. Notice in the code that the value "ODBC" is passed in the GetConString method, returning the ODBC connection string:

    string connStr = GetConString(ConnType.ODBC);

    using (OdbcConnection conn = new OdbcConnection(connStr))
    {
    try
    {
    conn.Open();
    OdbcDataAdapter da = new OdbcDataAdapter();
    OdbcCommand cmd = new OdbcCommand("SELECT Name FROM Users", conn);
    cmd.CommandType = CommandType.Text;
    da.SelectCommand = cmd;
    DataSet ds = new DataSet("Users");
    da.Fill(ds);
    listBox1.DataSource = ds.Tables[0];
    dataGridView1.DataSource = ds.Tables[0];
    listBox1.DisplayMember = "Name";

    }
    catch (OdbcException ex)
    {
    MessageBox.Show(ex.Message.ToString());

    }
    }

  4. Run the project, and click the ODBC button. As in the previous example, the list box populates with the names from the Users table. The grid also populates with the same set of names (see Figure 2).

    Figure 2. Finished form with data

From these examples, you can see that connecting to and querying SQL Azure is no different from connecting to a local instance of SQL Server.

So far we have discussed connecting with ADO.NET and ODBC along with the different options we have with each, so let's continue the discussion and talk about using the SqlCmd utility.

Other -----------------
- Programming with SQL Azure : Application Deployment Factors
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 3)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 1)
- SQL Server 2008: SQL Server Web Services
- SQL Server 2008: SQL Server Service Broker - Related System Catalogs
- SQL Azure Backup Strategies (part 2)
- SQL Azure Backup Strategies (part 1) - Copying a Database
- SQL Server 2008: Troubleshooting SSB Applications with ssbdiagnose.exe
- SQL Server 2008: Service Broker Routing and Security
- Migrating Databases and Data to SQL Azure (part 9)
- Migrating Databases and Data to SQL Azure (part 8)
- Understanding Service Broker Constructs (part 5)
- Understanding Service Broker Constructs (part 4) - Creating the Conversation Initiator
- Migrating Databases and Data to SQL Azure (part 7)
- Migrating Databases and Data to SQL Azure (part 6) - Building a Migration Package
- Migrating Databases and Data to SQL Azure (part 5) - Creating an Integration Services Project
- Understanding Service Broker Constructs (part 3)
- Understanding Service Broker Constructs (part 2) - Creating Queues for Message Storage
- Understanding Service Broker Constructs (part 1) - Defining Messages and Choosing a Message Type
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us